// Package main implements a client for Greeter service.
package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"time"

	"google.golang.org/grpc"
	// pb "google.golang.org/grpc/examples/helloworld/helloworld"
	pb "gitee.com/xzknet/goWebActualCombat/chapter5/5.3-gRPC/protobuf"
)

const (
	defaultName = "world"
)

//
var (
	addr = flag.String("addr", "localhost:50051", "the address to connect to") // 用golang自带的flag.String来解析参数
	name = flag.String("name", defaultName, "Name to greet")                   // 用golang自带的flag.String来解析参数
)

func main() {
	flag.Parse()

	// fmt.Printf("%s %s = %v \n\n", *name, defaultName, *name == defaultName)
	if *name == defaultName {
		fmt.Println("启动命令：go run clientMain.go -name xingMing -addr 127.0.0.1:50051")
	}

	// Set up a connection to the server.
	conn, err := grpc.Dial(*addr, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewGreeterClient(conn)

	// Contact the server and print out its response.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r.GetMessage())

	r, err = c.SayHello4Zh(ctx, &pb.HelloRequest{Name: *name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("回复: %s", r.GetMessage())
}
